//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pss;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.PssPurcContractDao;
import com.aidong.model.wills.PssPurcContract;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"purcContract"})
public class PurcContractController extends BaseController {
    @Resource
    PssPurcContractDao pssPurcContractDao;

    public PurcContractController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("pss/purccontract/list")).addObject("PayMode", this.getTypeList("PayMode")).addObject("DeliverMode", this.getTypeList("DeliverMode"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "contractCode",required = false) String contractCode, @RequestParam(value = "payMode",required = false) String payMode, @RequestParam(value = "deliverMode",required = false) String deliverMode, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        if(!this.chkNull(status)) {
            cd.eq("status", status);
        } else {
            cd.gt("status", Integer.valueOf(0));
        }

        ConditionMap mv;
        if(!this.chkNull(contractCode)) {
            mv = new ConditionMap(1);
            mv.like("contractCode", "%" + contractCode + "%");
            cd.addSub(mv);
        }

        if(!this.chkNull(payMode)) {
            mv = new ConditionMap(1);
            mv.eq("payMode", payMode);
            cd.addSub(mv);
        }

        if(!this.chkNull(deliverMode)) {
            mv = new ConditionMap(1);
            mv.eq("deliverMode", deliverMode);
            cd.addSub(mv);
        }

        this.pssPurcContractDao.queryPageList(pg, cd, (new OrderSet()).asc("contractCode"));
        ModelAndView mv1 = new ModelAndView("pss/purccontract/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        PssPurcContract pssPurcContract = new PssPurcContract();
        pssPurcContract.setStatus(Integer.valueOf(1));
        ModelAndView mv = new ModelAndView("pss/purcContract/edit");
        mv.addObject("pssPurcContract", pssPurcContract);
        mv.addObject("PayMode", this.getTypeList("PayMode"));
        mv.addObject("providerList", this.getTypeList("providerList"));
        mv.addObject("DeliverMode", this.getTypeList("DeliverMode"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("pssPurcContract") PssPurcContract pssPurcContract) {
        if(this.chkNull(pssPurcContract.getContractCode())) {
            return this.getResult(false, "请输入合同编号");
        } else if(this.chkNull(pssPurcContract.getProviderCode())) {
            return this.getResult(false, "请选择供应商编号");
        } else {
            if(this.chkNull(pssPurcContract.getStatus())) {
                pssPurcContract.setStatus(Integer.valueOf(1));
            }

            if(this.pssPurcContractDao.exists(pssPurcContract.getContractCode())) {
                return this.getResult(false, "合同编号已存在");
            } else {
                pssPurcContract.setCreateUser(this.getLoginUserCode());
                pssPurcContract.setCreateTime(new Date());
                pssPurcContract.setUpdateTime(new Date());

                try {
                    this.pssPurcContractDao.add(pssPurcContract);
                    this.reloadTypeList("pssPurcContract");
                    this.addUserLog("新增合同", "合同编号:" + pssPurcContract.getContractCode());
                } catch (Exception var3) {
                    this.log.error("add PurcContract", var3);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("contractCode") String contractCode) {
        PssPurcContract pssPurcContract = (PssPurcContract)this.pssPurcContractDao.getById(contractCode);
        if(pssPurcContract == null) {
            return this.showAlert("没有这个支付类型");
        } else {
            ModelAndView mv = new ModelAndView("pss/purccontract/edit");
            mv.addObject("pssPurcContract", pssPurcContract);
            mv.addObject("PayMode", this.getTypeList("PayMode"));
            mv.addObject("providerList", this.getTypeList("providerList"));
            mv.addObject("DeliverMode", this.getTypeList("DeliverMode"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("pssPurcContract") PssPurcContract pssPurcContract) {
        if(this.chkNull(pssPurcContract.getContractCode())) {
            return this.getResult(false, "没有这个合同");
        } else if(this.chkNull(pssPurcContract.getProviderCode())) {
            return this.getResult(false, "请选择供应商编号");
        } else {
            if(this.chkNull(pssPurcContract.getStatus())) {
                pssPurcContract.setStatus(Integer.valueOf(1));
            }

            PssPurcContract pssPurcContract1 = (PssPurcContract)this.pssPurcContractDao.getById(pssPurcContract.getContractCode());
            if(pssPurcContract1 == null) {
                return this.getResult(false, "没有这个合同");
            } else {
                pssPurcContract1.setProviderCode(pssPurcContract.getProviderCode());
                pssPurcContract1.setPayMode(pssPurcContract.getPayMode());
                pssPurcContract1.setBeginTime(pssPurcContract.getBeginTime());
                pssPurcContract1.setEndTime(pssPurcContract.getEndTime());
                pssPurcContract1.setDeliverMode(pssPurcContract.getDeliverMode());
                pssPurcContract1.setStatus(pssPurcContract.getStatus());
                pssPurcContract1.setUpdateTime(new Date());

                try {
                    this.pssPurcContractDao.update(pssPurcContract1);
                    this.reloadTypeList("pssPurcContract");
                    this.addUserLog("修改合同", "合同编号:" + pssPurcContract1.getContractCode());
                } catch (Exception var4) {
                    this.log.error("edit PurcContract", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("contractCode") String contractCode) {
        PssPurcContract pssPurcContract = (PssPurcContract)this.pssPurcContractDao.getById(contractCode);
        if(pssPurcContract == null) {
            return this.getResult(false, "没有这个合同");
        } else {
            try {
                this.pssPurcContractDao.updateStatus(contractCode, 0);
                this.reloadTypeList("pssPurcContract");
                this.addUserLog("删除合同", "合同编号:" + pssPurcContract.getContractCode());
            } catch (Exception var4) {
                this.log.error("del PurcContract", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}
